<template>
  <div class="global-padding">
    <div style="margin-bottom: 10px;">
      <el-input v-model="searchValue" placeholder="请输入内容" style="width: 200px;margin-right: 10px;" />
      <el-date-picker
        v-model="datePickerValue"
        type="daterange"
        align="right"
        unlink-panels
        value-format="yyyy-MM-dd"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        style="margin-right: 10px;"
        @change="datePickerConfirm"
      />
      <el-button type="primary" style="margin-left: 10px;" @click="conditionSearch">条件搜索</el-button>
      <el-button type="success" style="margin-left: 10px;" @click="handleBrandAdd">添加品牌</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="carTemplateId"
        label="ID"
        width="50"
      />
      <el-table-column
        fixed
        prop="createTime"
        label="日期"
      />
      <el-table-column
        prop="carBrand"
        label="品牌"
      />
      <el-table-column
        prop="carCapacity"
        label="车容量"
      />
      <el-table-column
        fixed="right"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleSubAdd(scope.row)">添加子项</el-button>
          <el-button type="text" size="small" @click="confirmDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="编辑" :visible.sync="updateDialogFormVisible">
      <el-table
        :data="subTableData"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          prop="carTemplateId"
          label="ID"
          width="50"
        />
        <el-table-column
          fixed
          prop="createTime"
          label="日期"
        />
        <el-table-column
          label="车型"
        >
          <template slot-scope="subscope">
            <el-input v-model="subscope.row.carBrand" placeholder="请输入内容" />
          </template>
        </el-table-column>
        <el-table-column
          label="车容量"
        >
          <template slot-scope="subscope">
            <el-input-number v-model="subscope.row.carCapacity" style="width: 150px;" :min="1" :max="10" label="车容量" />
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
        >
          <template slot-scope="subscope">
            <el-button type="text" size="small" @click="handleSubUpdate(subscope.row)">更新</el-button>
            <el-button type="text" size="small" @click="handleSubDelete(subscope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加子项" :visible.sync="subAddDialogFormVisible">
      <el-form>
        <el-form-item label="型号">
          <el-input v-model="subAddForm.carBrand" style="width: 250px;" autocomplete="off" />
        </el-form-item>
        <el-form-item label="车容量">
          <el-input-number v-model="subAddForm.carCapacity" style="width: 150px;" :min="1" :max="10" label="车容量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="subAddDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmSubAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加品牌" :visible.sync="addDialogFormVisible">
      <el-form>
        <el-form-item label="品牌">
          <el-input v-model="addForm.carBrand" style="width: 250px;" autocomplete="off" />
        </el-form-item>
        <el-form-item label="车容量">
          <el-input-number v-model="addForm.carCapacity" style="width: 150px;" :min="1" :max="10" label="车容量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <div style="margin-top: 10px;text-align: center;">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="curPage"
        :page-size="pageSize"
        :total="total"
        @current-change="curPageChange"
      />
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      optionValue: '',
      curPage: 1,
      pageSize: 8,
      total: 1,
      tableData: [],
      subTableData: [],
      dateField: 'create_time',
      datePickerValue: '',
      searchField: 'car_brand',
      searchValue: '',
      curRow: {},
      curSubRow: {},
      updateDialogFormVisible: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      subAddDialogFormVisible: false,
      addDialogFormVisible: false,
      subAddForm: {
        carBrand: '',
        carCapacity: 4
      },
      addForm: {
        carBrand: '',
        carCapacity: 4
      }
    }
  },
  mounted() {
    this.reqListData()
  },
  methods: {
    handleUpdate(row) {
      console.log(row)
      this.curRow = row
      this.updateDialogFormVisible = true
      this.reqSubListData()
    },
    curPageChange(v) {
      console.log(v)
      this.curPage = v
      this.reqListData()
    },
    datePickerConfirm(v) {
      console.log(v)
    },
    selectOptionChange(v) {
      console.log(v)
    },
    reqListData() {
      request.get('carTemplate/getBrandList', { params: {
        curPage: this.curPage,
        pageSize: this.pageSize,
        dateField: this.dateField,
        startDate: this.datePickerValue ? this.datePickerValue[0] : '',
        endDate: this.datePickerValue ? this.datePickerValue[1] : '',
        searchField: this.searchField,
        search: this.searchValue
      }}).then(res => {
        this.tableData = []
        console.log(res)
        for (const v of res.records) {
          this.tableData.push(v)
        }
        this.curPage = res.current
        this.pageSize = res.size
        this.total = res.total
      })
    },
    reqSubListData() {
      request.get(`carTemplate/getSerialList/${this.curRow.carTemplateId}`).then(res => {
        this.subTableData = []
        console.log(res)
        for (const v of res) {
          this.subTableData.push(v)
        }
      })
    },
    conditionSearch() {
      this.reqListData()
    },
    confirmDelete(row) {
      this.$alert('确定删除吗', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if (action === 'confirm') {
            request.delete(`/carTemplate/deleteCarTemplate/${this.curRow.carTemplateId}`).then(res => {
              this.$message({
                type: 'info',
                message: `删除成功`
              })
              this.reqListData()
            })
          }
        }
      })
    },
    handleSubUpdate(subrow) {
      request.put(`/carTemplate/updateBrand`, {
        'carBrand': subrow.carBrand,
        'carCapacity': subrow.carCapacity,
        'carTemplateId': subrow.carTemplateId
      }).then(res => {
        this.$message({
          type: 'info',
          message: `更新成功`
        })
        this.reqListData()
      })
    },
    handleSubDelete(subrow) {
      this.$alert('确定删除吗', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if (action === 'confirm') {
            request.delete(`/carTemplate/deleteCarTemplate/${subrow.carTemplateId}`).then(res => {
              this.$message({
                type: 'info',
                message: `删除成功`
              })
              this.reqSubListData()
            })
          }
        }
      })
    },
    handleConfirmSubAdd() {
      if (this.subAddForm.carBrand === '') {
        this.$message({
          type: 'warning',
          message: `请输入型号`
        })
        return
      }
      if (this.subAddForm.carCapacity <= 0 || this.subAddForm.carCapacity > 10) {
        this.$message({
          type: 'warning',
          message: `车容量范围 1-10`
        })
        return
      }
      request.post(`/carTemplate/addSubBrand`, {
        'carBrand': this.subAddForm.carBrand,
        'carCapacity': this.subAddForm.carCapacity,
        'parentId': this.curSubRow.carTemplateId
      }).then(res => {
        this.$message({
          type: 'info',
          message: `添加成功`
        })
        this.subAddDialogFormVisible = false
        this.subAddForm.carBrand = ''
        this.subAddForm.carCapacity = 4
        this.curSubRow.carTemplateId = 0
      })
    },
    handleSubAdd(row) {
      this.subAddDialogFormVisible = true
      this.curSubRow = row
    },
    handleBrandAdd() {
      this.addDialogFormVisible = true
    },
    handleConfirmAdd() {
      if (this.addForm.carBrand === '') {
        this.$message({
          type: 'warning',
          message: `请输入型号`
        })
        return
      }
      if (this.addForm.carCapacity <= 0 || this.addForm.carCapacity > 10) {
        this.$message({
          type: 'warning',
          message: `车容量范围 1-10`
        })
        return
      }
      request.post(`/carTemplate/addBrand`, {
        'carBrand': this.addForm.carBrand,
        'carCapacity': this.addForm.carCapacity
      }).then(res => {
        this.$message({
          type: 'info',
          message: `添加成功`
        })
        this.addDialogFormVisible = false
        this.reqListData()
        this.addForm.carBrand = ''
        this.addForm.carCapacity = 4
      })
    }
  }
}
</script>
